<script>
$(document).ready(function () {
  var table = table_main("#queue_table");
  $("#all").click(function () { //select all button
    select_all();
  });
  $("#batch-spam").bind('click', function (e) { //batch spam
    batch_nav("batch_spam");
  });
  $("#batch-publish").bind('click', function (e) { //batch publish
    batch_nav("batch_publish");
  });
  $("#batch-ban").bind('click', function (e) { //batch ban
    batch_nav("batch_ban");
  });
  $("#batch-unban").bind('click', function (e) { //batch unban
    batch_nav("batch_unban");
  });
  $("#delete-batch").bind('click', function (e) { // batch delete
    batch_nav("batch_delete");
  });
});
</script>

<div class="py-3 pl-3 w-100">
  <% if @tags_followed.length == 0 %> 
    <span class="text-secondary">You are not following any tag, Click <a class="badge badge-pill badge-info" href="/subscriptions">here</a> to follow. </span>
  <% else %>
    <a class="fa fa-plus-circle text-info" href="/subscriptions"> </a> 
  <% end %>
  <% @tags_followed.limit(20).each do |tag| %>
    <a class="btn btn-xs my-1 btn-<% if params[:tag] ==  tag.tagname %>info<%else%>light border<%end%> border-curve" href="/spam2/queue/filter/<%= tag.tagname %>"><%= tag.tagname %> <span class="badge badge-pill badge-<% if params[:tag] ==  tag.tagname %>light<%else%>info<%end%>" data-toggle="tooltip" data-placement="top" title="Filter Nodes by Tag"><%= Tag.followers(tag.tagname).where('rusers.role = ?', 'moderator').or(Tag.followers(tag.tagname).where('rusers.role = ?', 'admin')).size %></span></a> 
  <% end %>
  </div>
<div class="card" id="table-card">
  <div class="bg-light navbar navbar-expand">
    <ul class="nav navbar-expand navbar-nav-scroll">
      <li class="nav-item">
        <a class="btn nav-link text-secondary" data-toggle="tooltip" data-placement="top" title="Selected per page"> Selected <span id="select-count" class="badge badge-dark">0</span></a>
      </li>
      <li class="nav-item">
        <a href="/spam2/queue/filter/everything" class="btn nav-link  <% if params[:tag] == "everything" %> active<% else %> text-secondary<% end %>"><i class="fa fa-arrow-circle-o-up <% if params[:tag] == "everything" %> text-dark<% else %> text-secondary<% end %>" data-toggle="tooltip" data-placement="top" title="All Nodes which you are following"></i> All Tags</a>
      </li>
    </ul>
  </div>
  <div class="card-body" style="overflow-x:hidden;">
    <% unless @queue.empty? %>
    <div class="my-0 text-secondary small "><%= page_entries_info(@queue) %>s</div>
    <% end %>
    <table  id="queue_table" class="nowrap table table-hover" style="width:100%; text-align:left">
      <thead style="text-align:left;">
        <tr>
          <th><input type="checkbox"  id="selectall"/></th>
	  <th style="width:250px !important;"><%=translation('users.profile.title')%></th>
	  <th>Author</th>
          <th>Updated at</th>
          <th>Action</th>           
        </tr>       
      </thead>
      <tbody> 
        <% @queue.each do |node| %>
          <tr id="n<%= node.id %>">
            <td><input  class="selectedId" value="<%= node.nid %>" type="checkbox" /></td>
            <td style="width:250px !important;">
              <% if node.status == 1 %><i class="fa fa-check text-success"></i> <% elsif node.status == 0 %><i class="fa fa-ban text-danger"></i> <% elsif node.status == 4 %><i class="fa fa-circle text-primary"></i> <% end %> <% if params[:type] == 'flagged'%> <i class="fa fa-flag text-warning"><% end %>
              <a href="javascript:void(0);" class="text-dark font-weight-bold" id="node-hover" data-toggle="modal" data-target="#ninfo<%= node.id %>"><%= node.title.truncate(25) %></a><br />
               <span class="text-secondary small"><%= node.type.capitalize %> | <%= time_ago_in_words(node.created_at) %> ago</span>
            </td>
            <td>
              <a href="/profile/<%= node.author&.name %>" class="text-info"><%= node.author&.name.truncate(15) %></a><br>
               <span class="text-secondary small"><%= Node.where(uid: node.author&.id).count %> Nodes<span> <%= node.author&.new_contributor%>
             </td>
             <td> 
               <span class="text-dark"><%= time_ago_in_words(node.updated_at) %> ago</span>
            </td>
             <td>
               <a class="btn btn-xs border-curve font-weight-bold btn<% if node.status != 1 %>-success<% else %>-secondary disabled<% end %> publish" data-remote="true" href="/moderate/publish/<%= node.id %>" ><i class="fa fa-check-circle fa-white"></i> Publish post</a>
               <a class="btn btn-xs border-curve font-weight-bold btn<% if node.status != 0 %>-danger<% else %>-secondary disabled<% end %> spam" data-remote="true" href="/moderate/spam/<%= node.id %>"><i class="fa fa-ban fa-white"></i> Spam post</a> 
               <a class="btn btn-xs border-curve font-weight-bold btn-secondary ban a<%= node.author.id %>" <% if node.author.status == 0 %>style="display:none;"<% end %> data-remote="true" href="/ban/<%= node.author.id %>">Ban user</a>
               <a class="btn btn-xs border-curve font-weight-bold btn-secondary unban a-unban<%= node.author.id %>" <% if node.author.status == 1 %>style="display:none;"<% end %> data-remote="true" href="/unban/<%= node.author.id %>">Unban user</a>
               <a class="btn btn-xs border-curve font-weight-bold btn-warning unflag <% if node.flag == 0 %>disabled <% end %>" data-remote="true"href="/moderate/remove_flag_node/<%= node.id %>"> Unflag</a>
               <%= link_to "/notes/delete/#{node.id}", data: { confirm: "Are you sure you want to delete #{node.path}?" }, :remote => true, :class => "btn border-curve btn-sm font-weight-bold btn-light delete" do %>
                <i class="fa fa-trash text-dark"></i>
               <% end %>
               <script>
                $('#n<%= node.id %> .delete').bind('ajax:success', function(e){
                  $('#n<%= node.id %>').fadeOut()
                  notyNotification('relax', 3000, 'danger', 'topRight', 'Node deleted');
                });
                $('#n<%= node.id %> .publish').bind('ajax:success', function(e){
                  $('#n<%= node.id %>').hide()
                  notyNotification('relax', 3000, 'success', 'topRight', 'Node published');
                });
                $('#n<%= node.id %> .spam').bind('ajax:success', function(e){
                  $('#n<%= node.id %>').hide()
                  notyNotification('relax', 3000, 'success', 'topRight', 'Node spammed');
                });  
                $('.a<%= node.author.id %>.ban').bind('ajax:success', function(e){
                  $('.a<%= node.author.id %>').hide()  // ban toggle
                  $('.a-unban<%= node.author.id %>').show()
                  notyNotification('relax', 3000, 'danger', 'topRight', 'Author banned');
                });
                $('.a-unban<%= node.author.id %>.unban').bind('ajax:success', function(e){
                  $('.a-unban<%= node.author.id %>').hide()
                  $('.a<%= node.author.id %>').show()
                  notyNotification('relax', 3000, 'success', 'topRight', 'Author unbanned');
                });
                $('#n<%= node.id %> .unflag').bind('ajax:success', function(e){
                  $('#n<%= node.id %>').hide()
                  notyNotification('relax', 3000, 'warning', 'topRight', 'Node flagged');
                });  
                </script>
              </td>
            </tr>
            <div class="modal fade" id="ninfo<%= node.id %>"> 
              <div class="modal-dialog" >
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title"><a href="<%= node.path %>"><%= node.title %></a></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span>&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    <% node.tags.limit(4).each do |tag| %>
                      <span ><a class="badge badge-primary" href="/questions/tag/<%= tag.name %>"> <%= tag.name %> </a></span>
                    <% end %>
                    <br>
                    <%= node.body %>
                  </div>
                  <div class="modal-footer">
                    <%= time_ago_in_words(node.created_at) %> ago
                  </div>
                </div>
              </div>
            </div>
          <% end %>
        </tbody>
      </table>                       
    </div>
  </div>
</div>
<div class="page-table">
  <div class="float-right">
    <%= will_paginate @queue, :renderer => WillPaginate::ActionView::BootstrapLinkRenderer unless @unpaginated || @queue.empty? %>
  </div>
  <div class="float-left text-secondary">
    <% unless @queue.empty? %> Page number <%= @queue.current_page %> of <%= @queue.total_pages %> total Pages <% end %>
  </div>
</div>
